<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="blog_edit.html">写博客</a>
    <a href="login.html">登录</a>
</div>
<!-- 版心 -->
<div class="login-container">
    <!-- 中间的录框 -->
    <div class="login-dialog">
        <h3>注册</h3>
        <div class="row">
            <span>昵称</span>
            <input type="text" id="nickname">
        </div>
        <div class="row">
            <span>登录名</span>
            <input type="text" id="username">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" id="password">
        </div>
        <div class="row">
            <span>确认密码</span>
            <input type="password" id="password2">
        </div>
        <div class="row">
            <button id="submit" onclick="mySubmit()">提交</button>
        </div>
    </div>
</div>
<script>
    // 注册的数据提交 AJAX
    function mySubmit(){
        // 1.对输入的内容进行非空和正确性效验
        var nickname = jQuery("#nickname");
        var username = jQuery("#username");
        var password = jQuery("#password");
        var password2 = jQuery("#password2");
        if(jQuery.trim(nickname.val())==""){
            alert("抱歉：请先输入昵称！");
            nickname.focus();
            return;
        }
        if(jQuery.trim(username.val())==""){
            alert("抱歉：请先输入登录名！");
            username.focus();
            return;
        }
        if(jQuery.trim(password.val())==""){
            alert("抱歉：请先输入密码！");
            password.focus();
            return;
        }
        if(jQuery.trim(password2.val())==""){
            alert("抱歉：请先输入确认密码！");
            password2.focus();
            return;
        }
        // 密码强度效验（密码的长度必须大于 8）【扩展，密码组合强度效验】
        if(password.val().length<8){
            alert("抱歉：密码强度太低，密码位数必须大于等于8位！");
            password.focus();
            return; 
        }
        if(password.val()!=password2.val()){
            alert("抱歉：两次输入的密码不一致，请检查！");
            password.focus();
            return;
        }
        // 2.发送 ajax 请求到后端
        jQuery.ajax({
            url:"reg",
            type:"POST",
            data:{
                "username":username.val(),
                "nickname":nickname.val(),
                "password":password.val()
            },
            success:function(data){
                if(data!=null && data.succ==200){
                    if(data.data==1){
                        // 注册成功
                        alert("恭喜：注册成功");
                        // 跳转到登录页面
                        location.href = "login.html";
                    }else{
                        // 注册失败
                        alert("抱歉：注册失败，"+data.msg);
                    }
                }else{
                  alert("后台程序出错！");  
                }
            }
        });
    }
</script>
</body>

</html>